import { createPost, listPost, fetchPost, checkOrder, listByPostUser, noticeList, updateNoticeRead } from '@/services/api';
import { message } from 'antd';
import router from 'umi/router';

export default {
  namespace: 'post',

  state: {
    list: {pagination: {}, list: []},
    detail: {},
    orderData: {},
    listHasMore: false,
    modalPayVisible: false,
    trackList: [],
    noticeList: {pagination: {}, list: []},
    postType: '0',
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(listPost, payload);
      yield put({
        type: 'save',
        payload: response.data,
      });
    },
    *fetchTrackList({ payload }, { call, put }) {
      const response = yield call(listByPostUser, payload);
      yield put({
        type: 'saveTrackList',
        payload: response.data,
      });
    },
    *fetchNotice({ payload }, { call, put }) {
      const response = yield call(noticeList, payload);
      yield put({
        type: 'saveNoticeList',
        payload: response.data,
      });
      yield put({ type: 'user/fetchCurrent' });
    },
    *updateNoticeRead({ payload }, { call, put }) {
      const response = yield call(updateNoticeRead, payload);
      yield put({type: 'user/fetchCurrent'});
    },
    *hideModal(_, { call, put }) {
      yield put({
        type: 'saveModalVisible',
        payload: false,
      });
    },
    *fetchPost({ payload }, { call, put }) {
      const response = yield call(fetchPost, payload);
      yield put({
        type: 'saveDetail',
        payload: response.data,
      });
    },
    *create({ payload }, { call, put }) {
      const response = yield call(createPost, payload.data);
      if(response.status === 1){
        message.success(response.info);
        yield put({type: 'savePostType', payload: payload.data.post_type});
        if(payload.data.post_type === '0') {
          yield put({
            type: 'saveOrderData',
            payload: response.data.data,
          });
          if(payload.data.platform === 'wechat') {
            payload.callback();
          }else {
            yield put({
              type: 'saveModalVisible',
              payload: true,
            });
          }
        } else {
          router.push('/list');
        }
      }else {
        message.error(response.info);
      }
    },
    *checkOrder({ payload }, { call, put }) {
      const response = yield call(checkOrder, payload);
      if(response.status === 1) {
        yield put({
          type: 'saveModalVisible',
          payload: false,
        });
        message.success('红包充值成功');
        router.push('/list');
      }
    },
  },

  reducers: {
    savePostType(state, action) {
      return {
        ...state,
        postType: action.payload,
      };
    },
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveDetail(state, action) {
      return {
        ...state,
        detail: action.payload,
      };
    },
    saveModalVisible(state, action) {
      return {
        ...state,
        modalPayVisible: action.payload,
      };
    },
    saveOrderData(state, action) {
      return {
        ...state,
        orderData: action.payload,
      };
    },
    saveTrackList(state, action) {
      return {
        ...state,
        trackList: action.payload,
      };
    },
    saveNoticeList(state, action) {
      return {
        ...state,
        noticeList: action.payload,
      };
    },
  },
};
